package gui.mainFrame.testConditionViewer.branchTable;

import gui.GUIController;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import testGenerator.Controller;
import testGenerator.testMethodModel.TestPath;

/**
 * Provides table for editing test values.
 * @author William Whitney
 */
public class BranchTable extends JPanel
{
    private static final long serialVersionUID = 1L;

    private JTable table;
    private final BranchTableModel tableModel;
    private final GUIController guiControl;
    private final Controller controller;

    /**
     * Default constructor.
     * @param controller 
     * @param guiControl
     * @param valueTableModel
     */
    public BranchTable(Controller controller, GUIController guiControl, BranchTableModel valueTableModel)
    {
        this.controller = controller;
        this.guiControl = guiControl;

        this.tableModel = valueTableModel;

        //Setup BranchTable
        this.setupTable();

        //Setup Panel
        this.buildView();
    }

    /**
     * Sets up the table.
     */
    private void setupTable()
    {
        table = new JTable(this.tableModel);
        table.getTableHeader().setReorderingAllowed(false);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        table.addMouseListener(getMouseListener());
    }

    /**
     * Sets up the JPanel
     */
    private void buildView()
    {
        this.setLayout(new BorderLayout());


        this.add(new JScrollPane(table), BorderLayout.CENTER);



    }

    private ActionListener getAddBranchBtnListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                TestPath newBranch = new TestPath();
                tableModel.addBranch(newBranch);
            }
        };

    }

    private ActionListener getEditBranchBtnListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent evt)
            {
                int selectedRow = table.getSelectedRow();
                if (selectedRow != -1)
                {
                    TestPath branch = tableModel.getBranchAtRow(selectedRow);
                    controller.setCurrBranch(branch);
                    guiControl.showTestCreator();
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Please Select a Branch.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        };
    }

    private ActionListener getDuplicateBranchBtnListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent evt)
            {
                int selectedRow = table.getSelectedRow();
                if (selectedRow != -1)
                {
                    tableModel.duplicateBranchAtRow(selectedRow);
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Please Select a Branch.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        };
    }

    private ActionListener getRemoveBranchBtnListener()
    {
        return new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent evt)
            {
                int selectedRow = table.getSelectedRow();
                if (selectedRow != -1)
                {
                    tableModel.deleteBranchRow(selectedRow);
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Please Select a Branch.", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        };
    }

    private MouseAdapter getMouseListener()
    {
        return new MouseAdapter()
        {

            @Override
            public void mouseClicked(MouseEvent evt)
            {
                int selectedRow = table.getSelectedRow();
                if (evt.getClickCount() == 2)
                {
                    //TODO Remove duplicate code
                    TestPath branch = tableModel.getBranchAtRow(selectedRow);
                    controller.setCurrBranch(branch);
                    guiControl.showTestCreator();
                }
            }
        };
    }
}
